package net.adriana.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import net.adriana.Fac;
import net.adriana.component.MyButton;
import net.adriana.component.MyFrame;
import net.adriana.esaytool.ClassCreate;

public class LastWindow extends MyFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextField url;
	private JTextField packageName;
	private MyButton start;
	public static JTextArea log;
	
	public LastWindow() {
		super(500, 340);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addLabels();
		addTexts();
		addTextArea();
		addStartBtn();
		addFileDialog();
		setVisible(true);
	}

	private void addLabels(){
		initLabel("创建地址:", 40, 15, 100, 50);
		initLabel("包名:", 40, 45, 50, 50);
	}

	private void addTexts(){
		url = new JTextField();
		url.setBounds(100, 30, 290, 20);
		packageName = new JTextField();
		packageName.setBounds(100, 60, 290, 20);
		content.add(url);
		content.add(packageName);
	}
	
	public void addTextArea(){
		JScrollPane sp = new JScrollPane();
		sp.setBounds(40, 100, 420, 160);
		content.add(sp);
		log = new JTextArea();
		log.setBounds(40, 100, 420, 160);
		log.setEditable(false);
		sp.setViewportView(log);
		log.getDocument().addDocumentListener(new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				if(Fac.isError){
					Fac.isError = false;
					previous.setEnabled(true);
					exit.setEnabled(true);
					start.setEnabled(true);
				}
				if(Fac.isDone){
					Fac.isDone = false;
					previous.setEnabled(true);
					exit.setEnabled(true);
					start.setEnabled(true);
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void addStartBtn(){
		start = new MyButton("生成");
		start.setBounds(400, 60, 55, 19);
		content.add(start);
		start.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(url.getText().equals("") || url.getText().equals(" ")){
					alert("创建路径不能为空", "错误");
					return;
				}
				start.setEnabled(false);
				previous.setEnabled(false);
				exit.setEnabled(false);
				Fac.packageName = packageName.getText();
				ClassCreate creater = new ClassCreate();
				creater.create();
			}
		});
	}
	
	private void addFileDialog(){
		final MyButton browse = new MyButton("...");
		browse.setBounds(400,30,55,19);
		browse.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(false);
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if(chooser.showOpenDialog(browse) == JFileChooser.APPROVE_OPTION){
					File file = chooser.getSelectedFile();
					Fac.filePath = file.getAbsolutePath();
					url.setText(Fac.filePath);
				}
			}
		});
		content.add(browse);
	}
	
	public void alert(String msg,String title){
		JOptionPane.showMessageDialog(
				null, 
				msg, 
				title,
				JOptionPane.ERROR_MESSAGE);
	}
	
}

